<?php


namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use app\common\model\tang\Cost;
use app\common\model\tang\CostRule;
use app\common\model\tang\Merchant as MerchantModel;
use app\common\model\tang\Cost as CostModel;
use app\common\model\tang\CostType;
use app\common\model\tang\RepairMaterial;
use app\common\model\tang\RepairRecord as RecordModel;
use app\common\model\tang\StaffRule;
use app\common\model\tang\SubjectList;
use think\facade\Config;
use think\facade\Db;

class RepairList extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new CostModel();
    }

    public function index()
    {
        if (IS_AJAX){
            $where=[['type','=',6]];  // ,['cost_type_id','notin',[1, 2, 5]]
            return $this->model->getAdminPageData($this->param, 'id desc',$where);
        }
        $merchant_list =getMerchant();
        //获取菜单列表
        $this->assign(compact("merchant_list"));
        return $this->fetch();
    }
    public function detail()
    {
        $id = $this->request->param('id/d');
        //维修id
        $recordId = RecordModel::where('cost_id',$id)->value('id');
        $recordInfo = RecordModel::with(['repairType' => function ($e) {
            $e->field('id,name')->find();
        },'merchant','cost'])->where('id',$recordId)->find()->toArray();
        if(!empty($recordInfo['images'])){
            $recordInfo['images'] = explode(',',$recordInfo['images']);
        }
        if(!empty($recordInfo['dispose_image'])){
            $recordInfo['dispose_image'] = explode(',',$recordInfo['dispose_image']);
        }
        if(!empty($recordInfo['dispose_material'])){
            $material = explode(',',$recordInfo['dispose_material']);
            $allMoney= 0;
            foreach ($material as $k=>$v){
                $temp=explode(':',$v);
                $price = (new RepairMaterial())->field('id,price')->find($temp[0]);
                $singel = bcmul($price['price'],$temp[2],2);
                array_push($temp,$singel);
                array_push($temp,$price['price']);
                $allMoney+= bcmul($price['price'],$temp[2],2);
                $material[$k]=$temp;
            }
            $workPriceInfo = (new RepairMaterial())->field('id,name,price')->find(1);
            $workPrice = bcmul($workPriceInfo['price'],$recordInfo['working_hours'],2);
            $workItem[]= $workPriceInfo['id'];
            $workItem[]=$workPriceInfo['name'];
            $workItem[]=$recordInfo['working_hours'];
            $workItem[]=$workPriceInfo['price'];
            $workItem[]=$workPrice;
            $material[]=$workItem;
            $recordInfo['dispose_material'] = $material;
            $recordInfo['allMoney'] = bcadd($allMoney,$workPrice,2);
            $recordInfo['materialMoney'] = $allMoney;
            $recordInfo['hourMoney'] = $workPrice;
        }
        //查询订单是否已支付
        if($recordInfo['cost_id']){
            $costInfo = Cost::field('id,pay_status')->find($recordInfo['cost_id']);
            if(empty($costInfo)){
                $recordInfo['is_pay'] = 0;
            }else{
                $recordInfo['is_pay'] = $costInfo['pay_status'];
            }
        }
        $recordInfo['create_time'] = date('Y-m-d H:i',$recordInfo['create_time']);
        $recordInfo['order_time'] = date('Y-m-d H:i',$recordInfo['order_time']);
        $recordInfo['dispose_time'] = date('Y-m-d H:i',$recordInfo['dispose_time']);
        switch ($recordInfo['cost']['pay_status']){
            case -1 :
                $recordInfo['cost']['pay_status'] = '退费';
                break ;
            case 0 :
                $recordInfo['cost']['pay_status'] = '未支付';
                break ;
            case 1 :
                $recordInfo['cost']['pay_status'] = '已支付';
                break ;
        }
        $this->assign('info',$recordInfo);
        return $this->fetch();
    }
}